<?php

namespace app\stock\controller\Manage;

use think\Controller;
use think\Db;

class Highsea extends Controller
{
    // 模型
    protected $BusinessModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model("Business.Business");
    }

    public function index()
    {
        $highseaData = $this->BusinessModel->with(['source'])->whereNUll('adminid')->order('createtime DESC')->select();

        if (!$highseaData) {
            $this->error('暂无数据');
        }

        $this->success('查询成功', null, $highseaData);
    }

    public function info()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $Business = $this->BusinessModel->with(['source'])->find($busid);

        if (!$Business) {
            $this->error('用户不存在');
        }

        $this->success('返回数据成功', null, $Business);
    }

    public function admin()
    {
        $AdminData = model('Admin')->field('id,username')->select();

        if (!$AdminData) {
            $this->error('暂无数据');
        }

        $this->success('返回数据成功', null, $AdminData);
    }

    public function allot()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $adminid = $this->request->param('adminid', 0, 'trim');

        $action = $this->request->param('action', '', 'trim');

        $Business = $this->BusinessModel->find($busid);

        if (!$Business) {
            $this->error('用户不存在');
        }

        $admin = model('Admin')->find($adminid);

        if (!$admin) {
            $this->error('管理员不存在');
        }

        if ($admin['status'] !== 'normal') {
            $this->error('当前账号已禁用');
        }

        if (!empty($Business['adminid'])) {
            $this->error('用户已被领取');
        }

        /* 
            更新用户表 => adminid

            用户领取表 => 新增一条记录
        */

        // 开启事务
        Db::startTrans();

        $data = [
            'id' => $busid,
            'adminid' => $adminid
        ];

        $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($data);

        if ($BusinessStatus === FALSE) {
            $this->error('更新用户数据失败');
        }

        // 新增领取记录
        $receiveData = [
            'applyid' => $adminid,
            'status' => $action === 'apply' ? 'apply' : 'allot',
            'busid' => $busid
        ];

        $ReceiveStatus = model('Business.Receive')->validate('common/Business/Receive')->save($receiveData);

        if ($ReceiveStatus === FALSE) {
            Db::rollback();
            $this->error(model('Business.Receive')->getError());
        }

        $msg = $action === 'apply' ? '申请' : '领取';

        if ($ReceiveStatus === FALSE || $BusinessStatus === FALSE) {
            Db::rollback();
            $this->error($msg . '失败');
        } else {
            Db::commit();

            $this->success($msg . '成功');
        }
    }

    public function del()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $adminid = $this->request->param('adminid', 0, 'trim');

        $Business = $this->BusinessModel->find($busid);

        if (!$Business) {
            $this->error('用户不存在');
        }

        $admin = model('Admin')->find($adminid);

        if (!$admin) {
            $this->error('管理员不存在');
        }

        if ($admin['status'] !== 'normal') {
            $this->error('当前账号已禁用');
        }

        $result = $this->BusinessModel->destroy($busid);

        if ($result === FALSE) {
            $this->error('删除失败');
        } else {
            $this->success('删除成功');
        }
    }
}
